﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace XCrawler
{
    /// <summary>
    /// 列表的扩展
    /// </summary>
    public static class ListExtensions
    {
        #region IEnumerable<T> 集合操作
        /// <summary>
        /// 向集合中添加元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="value"></param>
        public static void Add<T>(this IEnumerable<T> collection, T value)
        {
            var list = collection as List<T>;
            if (list != null) list.Add(value);
        }

        /// <summary>
        /// 从集合中删除元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="value"></param>
        public static void Remove<T>(this IEnumerable<T> collection, T value)
        {
            var list = collection as List<T>;
            if (list != null) list.Remove(value);
        }
        /// <summary>
        /// 检索集合中是否包含某个元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool Contains<T>(this IEnumerable<T> collection, T value)
        {
            var list = collection as List<T>;
            return list != null && list.Contains(value);
        }

        /// <summary>
		/// Iterates through all sequence and performs specified action on each
		/// element
		/// </summary>
		/// <typeparam name="T">Sequence element type</typeparam>
		/// <param name="enumerable">Target enumeration</param>
		/// <param name="action">Action</param>
		/// <exception cref="System.ArgumentNullException">One of the input agruments is null</exception>
		public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            foreach (var elem in enumerable)
            {
                action(elem);
            }
        }

        /// <summary>
        /// Iterates through all sequence and performs specified action on each
        /// element
        /// </summary>
        /// <typeparam name="T">Sequence element type</typeparam>
        /// <param name="enumerable">Target enumeration</param>
        /// <param name="action">Action</param>
        /// <exception cref="System.ArgumentNullException">One of the input agruments is null</exception>
        public static async Task ForEach<T>(this IEnumerable<T> enumerable, Func<T, Task> action)
        {
            foreach (var elem in enumerable)
            {
                await action(elem).ConfigureAwait(false);
            }
        }
        #endregion


        #region Hashtable 集合操作
        /// <summary>
        /// 设置 Hashtable
        /// key 存在则更新，不存在则新增
        /// </summary>
        /// <param name="hash"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetValue(this Hashtable hash, string key, object value)
        {
            if (hash.ContainsKey(key))
            {
                hash[key] = value;
            }
            else
            {
                hash.Add(key, value);
            }
        }

        /// <summary>
        /// 获取 Hashtable 中的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hash"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T GetValue<T>(this Hashtable hash, string key, T value)
        {
            return (T)Convert.ChangeType(hash[key] != null ? hash[key] : value, typeof(T));
        }
        #endregion


        #region HttpHeaders 集合操作

        /// <summary>
        /// 获取HttpHeaders的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hash"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T GetValue<T>(this HttpHeaders hash, string key, T value)
        {
            var values = Enumerable.Empty<string>();
            return (T)Convert.ChangeType(hash.TryGetValues(key, out values) ? values.FirstOrDefault() : value, typeof(T));
        }

        #endregion



    }
}
